package com.shg.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.shg.bean.BookFile;
import com.shg.web.tools.ImageTool;
import com.shg.web.tools.WebTool;


@Controller
public class FileUploadAction{

	//上传的文件
	private File upload;
	//文件的名称
	private String uploadFileName;
	//文件的类型
	private String uploadContentType;
	//logo标示
	private String logo;
	//图片保存路径
	private String imagePath;
	//压缩图片路径
	private String imagePath_80;
	//
	private String imagePath_300;
	//图片压缩
	private Integer width_300=300;
	//所有的文件
	private List<BookFile>  bookFiles;
	//书籍文件
	private BookFile bookFile;
	//构建上传文件的目录
	private String showPath;
	//
	private String filePath;
	
	public String getLogo() {
		return logo;
	}
	public void setLogo(String logo) {
		this.logo = logo;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
	
	public String getImagePath_80() {
		return imagePath_80;
	}
	public void setImagePath_80(String imagePath_80) {
		this.imagePath_80 = imagePath_80;
	}
	public String getImagePath_300() {
		return imagePath_300;
	}
	public void setImagePath_300(String imagePath_300) {
		this.imagePath_300 = imagePath_300;
	}
	public List<BookFile> getBookFiles() {
		return bookFiles;
	}
	public void setBookFiles(List<BookFile> bookFiles) {
		this.bookFiles = bookFiles;
	}
	public BookFile getBookFile() {
		return bookFile;
	}
	public void setBookFile(BookFile bookFile) {
		this.bookFile = bookFile;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	/**
	 * 文件上传显示界面
	 */
	public String uploadUI(){
		return "uploadUI";
	}
	/**
	 * 店铺LOGO
	 */
	public String shopLogo(){
		return "shopLogo";
	}
	/**
	 * 文件上传的处理方法
	 * 文件的上传的时候如果logo的值是logo就需要几个压缩文件
	 */
	public String uploadFile(){
		//构建上传文件的目录
		SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd");
		String path = "/userfiles/"+WebTool.getLogin().getUserEmail()+"/logo/"+format.format(new Date());
		//获取文件上传所在位置的真实目录
		String savePaht =  ServletActionContext.getServletContext().getRealPath(path);
		//如果该上传文件的目录不存在就会创建一个目录
		File file=new File(savePaht);
		if(!file.exists())
			file.mkdirs();
		//获取上传文件的后缀 xxxx.png
		String ext=uploadFileName.substring(uploadFileName.lastIndexOf("."));
		//给上传文件命名uuid.png
		String name = UUID.randomUUID().toString();
		String fileName = name+ext;
		//读写上传文件
		try {
			FileInputStream inputStream=new FileInputStream(upload);
			byte [] b=new byte[inputStream.available()];
			inputStream.read(b);
			FileOutputStream outputStream=new FileOutputStream(new File(savePaht, fileName));
			outputStream.write(b);
			//关闭流
			outputStream.close();
			inputStream.close();
			//
			setImagePath(path+"/"+fileName);
			//讲上传的图片压缩一份（只对Logo图片进行）300px
			String oldImage=savePaht+"/"+fileName;
			String newImage=savePaht+"/"+"300";
			File f=new File(newImage);
			if(!f.exists())
				f.mkdirs();
			String filepath=newImage+"/"+name+"_300"+ext;
				
			ImageTool.saveMinPhoto(oldImage, filepath, width_300, 1);
			setImagePath_300(path+"/300/"+name+"_300"+ext);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	/**
	 * 上传店铺的Logo
	 * @return
	 */
	public String uploadShopLogo(){
		//构建上传文件的目录
		String path = "/userfiles/shoplogo";
		//获取文件上传所在位置的真实目录
		String savePaht =  ServletActionContext.getServletContext().getRealPath(path);
		//如果该上传文件的目录不存在就会创建一个目录
		File file=new File(savePaht);
		if(!file.exists())
			file.mkdirs();
		//获取上传文件的后缀 xxxx.png
		String ext=uploadFileName.substring(uploadFileName.lastIndexOf("."));
		//给上传文件命名uuid.png(登录用户的ID也就是店铺的ID)
		String name = WebTool.getLogin().getId();
		String fileName = name+ext;
		//读写上传文件
		try {
			String filepath=savePaht+"/"+fileName;
			ImageTool.saveMinPhoto(upload.getCanonicalPath(), filepath, 80, 1);
			this.logo=path+"/"+fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "finish";
	}
	/**
	 * 书籍文件
	 */
	public String bookFile(){
		return "bookFile";
	}
	/**
	 * 上传书籍文件
	 */
	public String uploadBook(){
		showPath= "/bookFile/"+WebTool.getLogin().getUserEmail();
		//获取文件上传所在位置的真实目录
		String savePaht =  ServletActionContext.getServletContext().getRealPath(showPath);
		//如果该上传文件的目录不存在就会创建一个目录
		File file=new File(savePaht);
		if(!file.exists())
			file.mkdirs();
		String filepath=savePaht+"/"+uploadFileName;
		file=new File(filepath);
		int i=1;
		String newName=uploadFileName;
		do{
			if(file.exists()){
				//给上传文件命名uuid.png(登录用户的ID也就是店铺的ID)
				String name = uploadFileName.substring(0,uploadFileName.lastIndexOf("."))+"("+i+")";
				//获取上传文件的后缀 xxxx.png
				String ext=uploadFileName.substring(uploadFileName.lastIndexOf("."));
				filepath =savePaht+"/"+ name+ext;
				file=new File(filepath);
				i++;
				newName=name+ext;
				continue;
			}
			break;
		}while(true);
		//读写上传文件
		try {
			
			FileUtils.copyFile(upload, new File(filepath));
			filePath=showPath+"/"+newName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "uploadBook";
	}
	/**
	 * 查询注定目录下的文件
	 */
	public String showBook(){
		showPath= "/bookFile/"+WebTool.getLogin().getUserEmail();
		String savePaht =  ServletActionContext.getServletContext().getRealPath(showPath);
		//获取该用户上传的所有文件
		File[] fs=new File(savePaht).listFiles();
		if(fs!=null){
			bookFiles=new ArrayList<BookFile>();
			for(int j=0;j<fs.length;j++){
				BookFile bookFile=new BookFile();
				//文件的名称
				bookFile.setName(fs[j].getName());
				//文件的绝对路径
				bookFile.setFilePath(fs[j].getPath().replace("\\", "/"));
				//文件的最近修改时间
				Date date=new Date(fs[j].lastModified());
				SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				bookFile.setTime(dateFormat.format(date));
				//添加到集合
				bookFiles.add(bookFile);
			}
		}else{
			if(bookFiles!=null)
				bookFiles.clear();
		}
		return "showBook";
	}
	/**
	 * 删除书籍
	 */
	public String deleteBook(){
		File file=new File(bookFile.getFilePath());
		file.delete();
		return showBook();
	}
}
